﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infra.Entity;


namespace Infra.Common.DAO
{
   public class EmailDAL
    {


        public static void Salvar(PEREMAIL email)
        {
           
            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                if (email.CODEMAIL != 0)
                {
                    //Alterar o utilizador
                    var Ema = dc.PEREMAILs.Single(mail => mail.CODEMAIL == email.CODEMAIL);
                    Ema.CODPESSOA = email.CODPESSOA;
                    Ema.CODTIPOEMAIL = email.CODTIPOEMAIL;
                    Ema.STREMAIL = email.STREMAIL;
                    dc.SubmitChanges();
                }
                else
                {
                    //Gravar utilizador
                    dc.PEREMAILs.InsertOnSubmit(email);
                    dc.SubmitChanges();
                }
            }
        }


        public static void Excluir(int idEmail)
        {
            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                var ema = dc.PEREMAILs.Single(m => m.CODEMAIL == idEmail);
                dc.PEREMAILs.DeleteOnSubmit(ema);
                dc.SubmitChanges();
            }
        }




        public static List<PEREMAIL> Pesquisar(int Codigo, string email)
        {
            List<PEREMAIL> ListaEmail = null;

            using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
            {
                var ema = from SP in dc.PEREMAILs
                              where ((Codigo.Equals(0) || Codigo.Equals(SP.CODEMAIL)) &&
                              (email.Equals(string.Empty) || email.Equals(SP.STREMAIL.Contains(email))))
                              select SP;
                ListaEmail = ema.ToList();
            }

            return ListaEmail;
        }
    }
}
